package com.dog.threesixty;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dog.utils.Constants;
import com.dog.utils.Downloader;
import com.dog.utils.Utils;
import com.inapp.util.BillingService;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class VettalkActivity extends Activity implements OnClickListener {

	private RelativeLayout imageViewContainer;
	private RelativeLayout recallLayout;
	private SharedPreferences sharedPreferences;

	private ImageView imageView;
	private TextView nameView;
	private ImageView viewButton;
	private ImageView backButton;

	private int x2;
	private int x;
	private int y;
	private int y2;

	private int imageViewWidth;
	private int imageViewHeight;
	private float factor;

	private String lastLayer = "General";

	private List<JSONArray> pointList;

	private boolean isPack2Downloaded;
	private boolean isPack3Downloaded;
	private boolean isPack4Downloaded;
	private boolean isPack5Downloaded;
	private boolean isMainBundleDownloaded;

	private boolean zoomedIn;
	private String jsonFile = "vetTalk_general_body.json";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {

			super.onCreate(savedInstanceState);
			setContentView(R.layout.layout_vettalk);

			try {
				sharedPreferences = PreferenceManager
						.getDefaultSharedPreferences(this);
				sharedPreferences
						.registerOnSharedPreferenceChangeListener(ospcl);
			} catch (Exception e) {
				e.printStackTrace();
			}

			imageViewContainer = (RelativeLayout) findViewById(R.id.image_view_container);
			recallLayout = (RelativeLayout) findViewById(R.id.recallLayout);

			checkPurchase();

			nameView = (TextView) findViewById(R.id.nameView);
			nameView.setText("General");

			imageView = (ImageView) findViewById(R.id.imageView);
			imageView.setOnClickListener(this);

			viewButton = (ImageView) findViewById(R.id.viewButton);
			viewButton.setOnClickListener(this);

			backButton = (ImageView) findViewById(R.id.backButton);
			backButton.setOnClickListener(this);

			imageViewContainer.post(new Runnable() {
				@Override
				public void run() {
					factor = Math.min(imageViewContainer.getWidth() / 480.0f,
							imageViewContainer.getHeight() / 320.0f);
					recallLayout.getLayoutParams().width = imageView
							.getLayoutParams().width = imageViewWidth = (int) (480 * factor);
					recallLayout.getLayoutParams().height = imageView
							.getLayoutParams().height = imageViewHeight = (int) (300 * factor);
				}
			});

			imageView.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View arg0, MotionEvent event) {
					switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						x = (int) (event.getX() / factor);
						y = (int) (event.getY() / factor);
						if (!zoomedIn) {
							tappedOnArea(x, y);
							zoomedIn = true;
						} else {
							// Toast.makeText(VettalkActivity.this,
							// "Now you can Parse", Toast.LENGTH_SHORT)
							// .show();
							Log.i("DREG", "X =" + x + " Y=" + y);
							parseJsonAndShow(x, y);
						}
						break;

					}
					return false;
				}
			});

			imageView.setImageURI(Uri.parse(getFilePath("general_first_view")));

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private final OnSharedPreferenceChangeListener ospcl = new OnSharedPreferenceChangeListener() {

		public void onSharedPreferenceChanged(final SharedPreferences sp,
				String key) {
			try {
				if (BillingService.KEY_IN_APP_PURCHASED_ITEM_CHANGED_TIME
						.equals(key)) {
					runOnUiThread(new Runnable() {
						public void run() {
							checkPurchase();
						}
					});
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	private void checkPurchase() {
		isMainBundleDownloaded = BillingService.isPurchased(sharedPreferences,
				Constants.BUNDLE_ITEM_ID);

		if (isMainBundleDownloaded) {
			isPack2Downloaded = true;
			isPack3Downloaded = true;
			isPack4Downloaded = true;
			isPack5Downloaded = true;

		} else {
			isPack2Downloaded = BillingService.isPurchased(sharedPreferences,
					Constants.PACK2_ITEM_ID);
			isPack3Downloaded = BillingService.isPurchased(sharedPreferences,
					Constants.PACK3_ITEM_ID);
			isPack4Downloaded = BillingService.isPurchased(sharedPreferences,
					Constants.PACK4_ITEM_ID);
			isPack5Downloaded = BillingService.isPurchased(sharedPreferences,
					Constants.PACK5_ITEM_ID);

		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(VettalkActivity.this,
					HomeScreenActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.viewButton:
			cycle();
			break;
		case R.id.backButton:
			if (zoomedIn) {
				goPreviousView();
			} else {
				Intent intent = new Intent(VettalkActivity.this,
						HomeScreenActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
			break;
		default:
			break;
		}

	}

	private void cycle() {
		checkPurchase();
		if (lastLayer.equalsIgnoreCase("General")) {
			// Change to Parent Coat
			tappedAnimationForLayer("coat_first_view");
			zoomedIn = false;
			lastLayer = "Types of Coats";
		} else if (lastLayer.equalsIgnoreCase("Types of Coats")) {
			// Change to
			tappedAnimationForLayer("tail_first_view");
			zoomedIn = false;
			lastLayer = "Types of Tails";
		} else if (lastLayer.equalsIgnoreCase("Types of Tails")) {

			// if pack 2 has been downloaded
			if (isPack2Downloaded) {
				// switch to ears
				tappedAnimationForLayer("ear_first_view");
				zoomedIn = false;
				lastLayer = "Types of Ears";
			}

			// else if pack 3 has been downloaded
			else if (isPack3Downloaded) {
				// switch to skeleton
				tappedAnimationForLayer("skeleton_first_view");
				zoomedIn = false;
				lastLayer = "Skeleton";
			}

			// else if pack 4 has been downloaded
			else if (isPack4Downloaded) {
				// switch to deep muscles
				tappedAnimationForLayer("tendon_first_view");
				zoomedIn = false;
				lastLayer = "Deep Muscles";
			}

			// else if pack 5 has been downloaded
			else if (isPack5Downloaded) {
				// swith to teeth
				tappedAnimationForLayer("TEETH_PARENT");
				zoomedIn = true;
				lastLayer = "Teeth";
				beginParsingForSection(1, lastLayer);

			}

			// else
			else {
				// switch back to general
				tappedAnimationForLayer("general_first_view");
				zoomedIn = false;
				lastLayer = "General";

			}

		} else if (lastLayer.equalsIgnoreCase("Skeleton")) {

			// if pack 3 has been downloaded
			if (isPack3Downloaded) {
				// switch to skeleton
				tappedAnimationForLayer("muscles_first_view");
				zoomedIn = false;
				lastLayer = "Superficial Muscles";
			}

			// else if pack 4 has been downloaded
			else if (isPack4Downloaded) {
				// switch to deep muscles
				tappedAnimationForLayer("tendon_first_view");
				zoomedIn = false;
				lastLayer = "Deep Muscles";
			}

			// else if pack 5 has been downloaded
			else if (isPack5Downloaded) {
				// swith to teeth
				tappedAnimationForLayer("TEETH_PARENT");
				zoomedIn = true;
				lastLayer = "Teeth";
				beginParsingForSection(1, lastLayer);

			}

			// else
			else {
				// switch back to general
				tappedAnimationForLayer("general_first_view");
				zoomedIn = false;
				lastLayer = "General";

			}

		} else if (lastLayer.equalsIgnoreCase("Superficial Muscles")) {

			// if pack 4 has been downloaded
			if (isPack4Downloaded) {
				// switch to deep muscles
				tappedAnimationForLayer("tendon_first_view");
				zoomedIn = false;
				lastLayer = "Deep Muscles";
			}

			// else if pack 5 has been downloaded
			else if (isPack5Downloaded) {
				// swith to teeth
				tappedAnimationForLayer("TEETH_PARENT");
				zoomedIn = true;
				lastLayer = "Teeth";
				beginParsingForSection(1, lastLayer);

			}

			// else
			else {
				// switch back to general
				tappedAnimationForLayer("general_first_view");
				zoomedIn = false;
				lastLayer = "General";
			}

		} else if (lastLayer.equalsIgnoreCase("Skull")) {

			// if pack 5 has been downloaded
			if (isPack5Downloaded) {
				// swith to teeth
				tappedAnimationForLayer("TEETH_PARENT");
				zoomedIn = true;
				lastLayer = "Teeth";

				beginParsingForSection(1, lastLayer);
			}

			else {
				// switch back to general
				tappedAnimationForLayer("general_first_view");
				zoomedIn = false;
				lastLayer = "General";

			}

		} else if (lastLayer.equalsIgnoreCase("Types of Ears")) {

			// switch to confomation skull and parse
			tappedAnimationForLayer("CONFORM_SKULL_PARENT");
			zoomedIn = true;
			lastLayer = "Types of Skulls";

			beginParsingForSection(1, lastLayer);

		} else if (lastLayer.equalsIgnoreCase("Types of Skulls")) {

			// switch to feet
			tappedAnimationForLayer("feet_first_view");
			zoomedIn = false;
			lastLayer = "Types of Feet";

		} else if (lastLayer.equalsIgnoreCase("Types of Feet")) {

			// switch to Front
			tappedAnimationForLayer("conform_front_first_view");
			zoomedIn = false;
			lastLayer = "Types of Conformation: Front";

		} else if (lastLayer.equalsIgnoreCase("Types of Conformation: Front")) {

			// switch to Side Front and parse
			tappedAnimationForLayer("CONFORM_SIDE_FRONT_PARENT");
			zoomedIn = true;
			lastLayer = "Types of Conformation: Side Front";

			beginParsingForSection(1, lastLayer);

		} else if (lastLayer
				.equalsIgnoreCase("Types of Conformation: Side Front")) {

			// switch to Hind and parse
			tappedAnimationForLayer("CONFORM_HIND_PARENT");
			zoomedIn = true;
			lastLayer = "Types of Conformation: Hind";

			beginParsingForSection(1, lastLayer);

		} else if (lastLayer.equalsIgnoreCase("Types of Conformation: Hind")) {

			// switch to Side Hind and parse
			tappedAnimationForLayer("CONFORM_SIDE_HIND_PARENT");
			zoomedIn = true;
			lastLayer = "Types of Conformation: Side Hind";

			beginParsingForSection(1, lastLayer);

		} else if (lastLayer
				.equalsIgnoreCase("Types of Conformation: Side Hind")) {

			// switch to Skeleton
			tappedAnimationForLayer("skeleton_first_view");
			zoomedIn = false;
			lastLayer = "Skeleton";

		} else if (lastLayer.equalsIgnoreCase("Deep Muscles")) {

			// switch to skull and parse
			tappedAnimationForLayer("SKULL_PARENT");
			zoomedIn = true;
			lastLayer = "Skull";

			beginParsingForSection(1, lastLayer);

		} else if (lastLayer.equalsIgnoreCase("Teeth")) {

			// switch to Brain and parse
			tappedAnimationForLayer("ORGANS_PARENT_brain");
			zoomedIn = true;
			lastLayer = "Brain";

			beginParsingForSection(1, lastLayer);

		} else if (lastLayer.equalsIgnoreCase("Brain")) {

			// switch to Heart and parse
			tappedAnimationForLayer("ORGANS_PARENT_heart");
			zoomedIn = true;
			lastLayer = "Heart";

			beginParsingForSection(1, lastLayer);

		} else if (lastLayer.equalsIgnoreCase("Heart")) {

			// switch to Intestine and parse
			tappedAnimationForLayer("ORGANS_PARENT_intestine");
			zoomedIn = true;
			lastLayer = "Intestine";

			beginParsingForSection(1, lastLayer);

		} else if (lastLayer.equalsIgnoreCase("Intestine")) {

			// switch to General
			tappedAnimationForLayer("general_first_view");
			zoomedIn = false;
			lastLayer = "General";

		}

		nameView.setText(lastLayer);

	}

	private void beginParsingForSection(int requestedSection,
			String requestedLayer) {

		// String jsonFile;

		if (requestedLayer.equalsIgnoreCase("Types of Coats")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_coat_first.json";

			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_coat_second.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("General")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_general_head.json";

			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_general_body.json";
			} else if (requestedSection == 3) {
				jsonFile = "vetTalk_general_forelimb.json";
			} else if (requestedSection == 4) {
				jsonFile = "vetTalk_general_hindlimb.json";
			}

		} else if (requestedLayer.equalsIgnoreCase("Types of Tails")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_tail_first.json";

			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_tail_second.json";
			} else if (requestedSection == 3) {
				jsonFile = "vetTalk_tail_third.json";
			}

		} else if (requestedLayer.equalsIgnoreCase("Types of Ears")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_ear_first.json";

			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_ear_second.json";
			}

		} else if (requestedLayer.equalsIgnoreCase("Types of Feet")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_feet_first.json";

			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_feet_second.json";
			}

		} else if (requestedLayer
				.equalsIgnoreCase("Types of Conformation: Front")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_front_first.json";

			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_front_second.json";
			}

		} else if (requestedLayer.equalsIgnoreCase("Types of Skulls")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_skull_conformation.json";

			}

		} else if (requestedLayer
				.equalsIgnoreCase("Types of Conformation: Side Front")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_front_side.json";

			}

		} else if (requestedLayer
				.equalsIgnoreCase("Types of Conformation: Hind")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_hind.json";

			}

		} else if (requestedLayer
				.equalsIgnoreCase("Types of Conformation: Side Hind")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_hind_side.json";

			}

		} else if (requestedLayer.equalsIgnoreCase("Skeleton")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_skeleton_head.json";

			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_skeleton_body.json";

			} else if (requestedSection == 3) {
				jsonFile = "vetTalk_skeleton_forelimb.json";

			} else if (requestedSection == 4) {
				jsonFile = "vetTalk_skeleton_hind.json";

			}

		} else if (requestedLayer.equalsIgnoreCase("Superficial Muscles")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_super_muscles_head.json";

			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_super_muscles_body.json";

			}

		} else if (requestedLayer.equalsIgnoreCase("Deep Muscles")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_deep_muscles_head.json";

			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_deep_muscles_body.json";

			}

		} else if (requestedLayer.equalsIgnoreCase("Skull")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_skull.json";

			}

		} else if (requestedLayer.equalsIgnoreCase("Teeth")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_teeth.json";

			}

		} else if (requestedLayer.equalsIgnoreCase("Heart")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_organs_heart.json";

			}

		} else if (requestedLayer.equalsIgnoreCase("Brain")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_organs_brain.json";

			}

		} else if (requestedLayer.equalsIgnoreCase("Intestine")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_organs_intestine.json";

			}
		}
	}

	private void parseJsonAndShow(int posX, int posY) {
		try {

			boolean isFound = false;
			Log.i("DREG", "File Name=" + jsonFile);
			pointList = new ArrayList<JSONArray>();

			JSONObject jo = new JSONObject(new String(Utils.getFileData(this,
					jsonFile)));
			JSONArray ja = jo.getJSONObject("root").getJSONArray("dog");
			for (int i = 0; i < ja.length(); i++) {
				JSONArray points = ja.getJSONObject(i).getJSONArray("points");
				pointList.add(points);
			}

			int targetPosition = -1;

			for (int j = 0; j < ja.length(); j++) {
				JSONArray points = pointList.get(j);
				for (int k = 0; k < points.length(); k++) {
					int x1 = (int) (points.getJSONObject(k).getInt("-x1"));
					int x2 = (int) (points.getJSONObject(k).getInt("-x2"));
					int y1 = (int) (points.getJSONObject(k).getInt("-y1"));
					int y2 = (int) (points.getJSONObject(k).getInt("-y2"));
					if (!isFound) {
						if ((posX >= x1 && posX <= x2)
								&& (posY >= y1 && posY <= y2)) {
							targetPosition = j;
							isFound = true;
							break;
						}
					}
				}
			}

			if (targetPosition != -1) {
				String fileName = ja.getJSONObject(targetPosition).getString(
						"-part");
				nameView.setText(fileName);
				tappedAnimationForLayer(fileName);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void tappedAnimationForLayer(String fileName) {
		try {
			Log.i("DREG", "File path = " + getFilePath(fileName));
			Bitmap bm = BitmapFactory.decodeFile(getFilePath(fileName));

			Bitmap ret = Bitmap.createBitmap(imageViewWidth, imageViewHeight,
					Bitmap.Config.ARGB_8888);
			Canvas comboImage = new Canvas(ret);
			Rect r1 = new Rect(0, 0, bm.getWidth(), bm.getHeight());
			Rect r2 = new Rect(0, 0, imageViewWidth, imageViewHeight);
			comboImage.drawBitmap(bm, r1, r2, null);
			comboImage.save();
			imageView.setImageBitmap(ret);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private String getFilePath(String fileName) {
		boolean isFound = false;
		fileName = fileName + ".png";
		String path = "";

		if (!isFound) {
			File pack1File[] = Downloader
					.getFileList(Constants.PACK1_DIRECTORY);
			if (pack1File != null) {
				for (int i = 0; i < pack1File.length; i++) {
					if (pack1File[i].getName().equalsIgnoreCase(fileName)) {
						isFound = true;
						path = pack1File[i].getAbsolutePath();
						return path;
					}
				}
			}
		}

		if (!isFound) {
			File pack2File[] = Downloader
					.getFileList(Constants.PACK2_DIRECTORY);
			if (pack2File != null) {
				for (int i = 0; i < pack2File.length; i++) {
					if (pack2File[i].getName().equalsIgnoreCase(fileName)) {
						isFound = true;
						path = pack2File[i].getAbsolutePath();
						return path;
					}
				}
			}
		}

		if (!isFound) {
			File pack3File[] = Downloader
					.getFileList(Constants.PACK3_DIRECTORY);

			if (pack3File != null) {
				for (int i = 0; i < pack3File.length; i++) {
					if (pack3File[i].getName().equalsIgnoreCase(fileName)) {
						isFound = true;
						path = pack3File[i].getAbsolutePath();
						return path;
					}
				}
			}
		}

		if (!isFound) {
			File pack4File[] = Downloader
					.getFileList(Constants.PACK4_DIRECTORY);

			if (pack4File != null) {
				for (int i = 0; i < pack4File.length; i++) {
					if (pack4File[i].getName().equalsIgnoreCase(fileName)) {
						isFound = true;
						path = pack4File[i].getAbsolutePath();
						return path;
					}
				}
			}
		}

		if (!isFound) {
			File pack5File[] = Downloader
					.getFileList(Constants.PACK5_DIRECTORY);
			if (pack5File != null) {
				for (int i = 0; i < pack5File.length; i++) {
					if (pack5File[i].getName().equalsIgnoreCase(fileName)) {
						isFound = true;
						path = pack5File[i].getAbsolutePath();
						return path;
					}
				}
			}
		}
		return path;
	}

	private void tappedOnArea(int x, int y) {

		Point point = new Point(x, y);

		if (lastLayer.equalsIgnoreCase("General") && !zoomedIn) {

			if (point.x >= 55 && point.x <= 150) {
				if (point.y >= 35 && point.y <= 120) {
					// parse xml

					// display head image
					tappedAnimationForLayer("GENERAL_PARENT_head");
					beginParsingForSection(1, lastLayer);

				}
			}

			if (point.x >= 152 && point.x <= 405) {
				if (point.y >= 90 && point.y <= 190) {
					// parse xml

					// display body image
					tappedAnimationForLayer("GENERAL_PARENT_body");
					beginParsingForSection(2, lastLayer);

				}
			}

			if (point.x >= 80 && point.x <= 240) {
				if (point.y >= 191 && point.y <= 315) {
					// parse xml

					// display forelimb image
					tappedAnimationForLayer("GENERAL_PARENT_frontleg");
					beginParsingForSection(3, lastLayer);

				}
			}

			if (point.x >= 242 && point.x <= 405) {
				if (point.y >= 191 && point.y <= 315) {
					// parse xml

					// display hindlimbs image
					tappedAnimationForLayer("GENERAL_PARENT_hindleg");
					beginParsingForSection(4, lastLayer);

				}
			}

		}/***** Conformation: Coats *****/

		else if (lastLayer.equalsIgnoreCase("Types of Coats") && !zoomedIn) {

			if (point.x > 25 && point.x < 465) {
				if (point.y >= 70 && point.y <= 180) {
					// parse xml

					// display top half
					tappedAnimationForLayer("COAT_PARENT_first");
					beginParsingForSection(1, lastLayer);

				} else if (point.y > 180 && point.y < 300) {
					// parse xml

					// display bottom half
					tappedAnimationForLayer("COAT_PARENT_second");
					beginParsingForSection(2, lastLayer);
				}
			}

		}/**** Conformation: Tails ****/

		else if (lastLayer.equalsIgnoreCase("Types of Tails") && !zoomedIn) {

			if (point.x > 25 && point.x < 465) {
				if (point.y >= 20 && point.y <= 140) {
					// parse xml

					// display top half
					tappedAnimationForLayer("TAIL_PARENT_first");
					beginParsingForSection(1, lastLayer);

				} else if (point.y > 141 && point.y < 218) {
					// parse xml

					// display middle half
					tappedAnimationForLayer("TAIL_PARENT_second");
					beginParsingForSection(2, lastLayer);
				} else if (point.y > 221 && point.y < 300) {
					// parse xml

					// bottom middle half
					tappedAnimationForLayer("TAIL_PARENT_third");
					beginParsingForSection(3, lastLayer);
				}
			}

		}/**** Conformation: Ears ****/
		else if (lastLayer.equalsIgnoreCase("Types of Ears") && !zoomedIn) {

			if (point.x > 20 && point.x < 465) {
				if (point.y >= 13 && point.y <= 138) {
					// parse xml

					// display top half
					tappedAnimationForLayer("EAR_PARENT_first");
					beginParsingForSection(1, lastLayer);

				} else if (point.y > 142 && point.y < 282) {
					// parse xml

					// display bottom half
					tappedAnimationForLayer("EAR_PARENT_second");
					beginParsingForSection(2, lastLayer);
				}

			}

		}
		/**** Conformation: Position Front ****/
		else if (lastLayer.equalsIgnoreCase("Types of Conformation: Front")
				&& !zoomedIn) {

			if (point.x > 20 && point.x < 465) {
				if (point.y >= 13 && point.y <= 138) {
					// parse xml

					// display top half
					tappedAnimationForLayer("CONFORM_FRONT_PARENT_second");
					beginParsingForSection(2, lastLayer);

				} else if (point.y > 142 && point.y < 282) {
					// parse xml

					// display bottom half

					tappedAnimationForLayer("CONFORM_FRONT_PARENT_first");
					beginParsingForSection(1, lastLayer);
				}

			}

		}

		/**** Conformation: Skull ******/

		/* parse this in switch method (noneed for zoom) */

		/**** Conformation: Feet ****/

		else if (lastLayer.equalsIgnoreCase("Types of Feet") && !zoomedIn) {

			if (point.x > 20 && point.x < 465) {
				if (point.y >= 3 && point.y <= 150) {
					// parse xml

					// display top half
					tappedAnimationForLayer("FEET_PARENT_first");
					beginParsingForSection(1, lastLayer);

				} else if (point.y > 158 && point.y < 269) {
					// parse xml

					// display bottom half
					tappedAnimationForLayer("FEET_PARENT_second");
					beginParsingForSection(2, lastLayer);
				}

			}

		}

		/**** Skeleton ****/
		else if (lastLayer.equalsIgnoreCase("Skeleton") && !zoomedIn) {

			if (point.x > 64 && point.x < 174) {
				if (point.y >= 23 && point.y <= 134) {
					// parse xml

					// display head
					tappedAnimationForLayer("SKELETON_PARENT_head");
					beginParsingForSection(1, lastLayer);

				}

			} else if (point.x > 177 && point.x < 376) {
				if (point.y >= 86 && point.y <= 178) {
					// parse xml

					// display body
					tappedAnimationForLayer("SKELETON_PARENT_body");
					beginParsingForSection(2, lastLayer);

				}

				if (point.y >= 179 && point.y <= 279) {

					if (point.x < 240) {

						// display forelimb
						tappedAnimationForLayer("SKELETON_PARENT_forelimb");
						beginParsingForSection(3, lastLayer);
					} else {

						// display hindlimb
						tappedAnimationForLayer("SKELETON_PARENT_hind");
						beginParsingForSection(4, lastLayer);
					}

				}

			}

		}/**** Superficial Muscles ****/
		else if (lastLayer.equalsIgnoreCase("Superficial Muscles") && !zoomedIn) {

			if (point.x > 60 && point.x < 159) {
				if (point.y >= 25 && point.y <= 163) {
					// parse xml

					// display head
					tappedAnimationForLayer("MUSCLES_PARENT_head");
					beginParsingForSection(1, lastLayer);

				}

			} else if (point.x > 163 && point.x < 385) {
				if (point.y >= 88 && point.y <= 293) {
					// parse xml

					// display body
					tappedAnimationForLayer("MUSCLES_PARENT_body");
					beginParsingForSection(2, lastLayer);

				}

			}

		}/**** Deep Muscles ****/
		else if (lastLayer.equalsIgnoreCase("Deep Muscles") && !zoomedIn) {
			if (point.x > 60 && point.x < 159) {
				if (point.y >= 25 && point.y <= 163) {
					// parse xml

					// display head
					tappedAnimationForLayer("TENDON_PARENT_head");
					beginParsingForSection(1, lastLayer);

				}

			} else if (point.x > 163 && point.x < 385) {
				if (point.y >= 88 && point.y <= 293) {
					// parse xml

					// display body
					tappedAnimationForLayer("TENDON_PARENT_body");
					beginParsingForSection(2, lastLayer);

				}

			}

		}/***** Skull ******/

		/**** parse this in the switch method (no need for zoom) ****/

		/***** Teeth ******/

		/**** parse this in the switch method (no need for zoom) ****/

		/***** Organs: Heart, Brain & Intestine ******/

		/**** parse this in the switch method (no need for zoom) ****/

		else /**** TAPPED ON PART ****/
		{
			zoomedIn = true;

			// for (VetTalkParts *part in partsArray)
			// {
			//
			// CGPoint beginningPoint = [part.beginningPoint CGPointValue];
			// CGPoint endPoint = [part.endPoint CGPointValue];
			//
			// beginningPoint.y = beginningPoint.y - 15;
			//
			// if (point.x >= beginningPoint.x && point.x <= endPoint.x)
			// {
			// if (point.y >= beginningPoint.y && point.y <= endPoint.y)
			// {
			//
			// if (![viewTitle.text isEqualToString:part.nameOfPart])
			// {
			// //set the title
			// [UIView beginAnimations:@"textChange" context:nil];
			// [UIView setAnimationDuration:0.5];
			// viewTitle.alpha = 0.0;
			// viewTitle.text = part.nameOfPart;
			// viewTitle.alpha = 1.0;
			// [UIView commitAnimations];
			//
			// }
			//
			// //change the image
			// CATransition *transition = [CATransition animation];
			// transition.duration = 0.5;
			// transition.timingFunction = [CAMediaTimingFunction
			// functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
			// transition.type = kCATransitionFade;
			// transition.delegate = self;
			//
			// [dogImage.layer addAnimation:transition forKey:nil];
			// dogImage.hidden = YES;
			// [dogImage setImage:[delegate
			// returnImageForName:part.nameOfPart]];
			// dogImage.hidden = NO;
			//
			// break;
			//
			//
			// }
			// }

		}

	}

	private void goPreviousView() {

		if (zoomedIn) {

			if (lastLayer.equalsIgnoreCase("General")) {
				tappedAnimationForLayer("general_first_view");
				zoomedIn = false;

			} else if (lastLayer.equalsIgnoreCase("Types of Coats")) {
				tappedAnimationForLayer("coat_first_view");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Types of Tails")) {
				tappedAnimationForLayer("tail_first_view");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Types of Ears")) {
				tappedAnimationForLayer("ear_first_view");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Types of Feet")) {
				tappedAnimationForLayer("feet_first_view");
				zoomedIn = false;
			} else if (lastLayer
					.equalsIgnoreCase("Types of Conformation: Front")) {
				tappedAnimationForLayer("conform_front_first_view");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Skeleton")) {
				tappedAnimationForLayer("skeleton_first_view");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Superficial Muscles")) {
				tappedAnimationForLayer("muscles_first_view");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Deep Muscles")) {
				tappedAnimationForLayer("tendon_first_view");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Types of Skulls")) {
				tappedAnimationForLayer("CONFORM_SKULL_PARENT");
				zoomedIn = false;
			} else if (lastLayer
					.equalsIgnoreCase("Types of Conformation: Side Front")) {
				tappedAnimationForLayer("CONFORM_SIDE_FRONT_PARENT");
				zoomedIn = false;
			} else if (lastLayer
					.equalsIgnoreCase("Types of Conformation: Hind")) {
				tappedAnimationForLayer("CONFORM_HIND_PARENT");
				zoomedIn = false;
			} else if (lastLayer
					.equalsIgnoreCase("Types of Conformation: Side Hind")) {
				tappedAnimationForLayer("CONFORM_SIDE_HIND_PARENT");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Skull")) {
				tappedAnimationForLayer("SKULL_PARENT");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Brain")) {
				tappedAnimationForLayer("ORGANS_PARENT_brain");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Heart")) {
				tappedAnimationForLayer("ORGANS_PARENT_heart");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Intestine")) {
				tappedAnimationForLayer("ORGANS_PARENT_intestine");
				zoomedIn = false;
			} else if (lastLayer.equalsIgnoreCase("Teeth")) {
				tappedAnimationForLayer("TEETH_PARENT");
				zoomedIn = false;
			}

			// set the title

			nameView.setText(lastLayer);

		} else {
			// [self.navigationController popViewControllerAnimated:YES];
		}

	}

}
